﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ServeNHL.Models
{
    [MetadataType(typeof(ExtUserInfoMetadata))]
    public partial class EXT_USER_INFO
    {
        public int saveExtUserInfo(UserEventViewModel extInfo, UserRoleModel org)
        {
            int extID = 0;

            if(extInfo.extUserInfo.EXT_USER_INFO_ID >= 0)
            {
                updateExtUserInfo(extInfo.extUserInfo);
            }
            else
            {
                insertExtUserInfo(extInfo.extUserInfo);
            }

            extID = extInfo.extUserInfo.EXT_USER_INFO_ID;

            return extID;

        }//end of public int saveExtUserInfo(UserEventViewModel extInfo, UserRoleModel org)

        public int insertExtUserInfo(EXTENDED_USER_INFO extDet)
        {
            int extInfoID = 0;

            using (var dbconn = new fclick_dbEntities())

            {
                var tempDet = new EXTENDED_USER_INFO {
                    EXT_USER_INFO_ID = extDet.EXT_USER_INFO_ID,
                    ORG_ID = extDet.ORG_ID,
                    NAME = extDet.NAME,
                    DESC = extDet.DESC,
                    DATA_FIELDS = extDet.DATA_FIELDS,
                    STATUS = extDet.STATUS,
                    CREATED_DATE = Helpers.createDate.getCreateDate(),
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate()

                };

                dbconn.EXTENDED_USER_INFO.Add(tempDet);
                dbconn.SaveChanges();

                extInfoID = tempDet.EXT_USER_INFO_ID;

            }//end of using (var dbconn = new fclick_dbEntities())
            return extInfoID;

        }// end of   public int insertExtUserInfo(EXT_USER_INFO_DETAILS extDet, ORGANIZATION org)

        public int updateExtUserInfo(EXTENDED_USER_INFO extDet) {
            int id = 0;

            using(var dbconn = new fclick_dbEntities())
            {
                var tempDet = new EXTENDED_USER_INFO();

                if (!String.IsNullOrEmpty(extDet.NAME))
                {
                    tempDet.NAME = extDet.NAME;
                }

                if (extDet.ORG_ID >= 0)
                {
                    tempDet.ORG_ID = extDet.ORG_ID;
                }
                
                if (!String.IsNullOrEmpty(extDet.DESC))
                {
                    tempDet.DESC = extDet.DESC;
                }

                if (!String.IsNullOrEmpty(extDet.DATA_FIELDS))
                {
                    tempDet.DATA_FIELDS = extDet.DATA_FIELDS;
                }

                if (!String.IsNullOrEmpty(extDet.STATUS))
                {
                    tempDet.STATUS = extDet.STATUS;
                }
               
                dbconn.EXTENDED_USER_INFO.Add(tempDet);
                dbconn.SaveChanges();

                id = tempDet.EXT_USER_INFO_ID;
                 
            }//end of using(var dbconn = new fclick_dbEntities())
            return id;
        }// end of public updateExtUserInfo(EXT_USER_INFO_DETAILS extDet, ORGANIZATION org)

    }
}